;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Sales.SalesOffset", [])
        .controller("SalesOffsetCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "EventBus", "$filter", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout, EventBus, $filter, UserContextService) {
            $scope.activeFlag = [true, false, false, false, false];
            $scope.datePickerFlag = [false, false];
            $scope.submitted = false;
            $scope.markInfo = {
                allChecked: false,
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.deliveryPageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.condition = {};
            $scope.orderCondition = {};
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false
            };
            $scope.dataList = [];
            $scope.deliveryNoticeList = [];
            $scope.detailInfo = {};
            $scope.steps = {
                curStep: 0,
                list: [{name: '选择发货通知单'}, {name: '生成销售冲销单'}]
            };
            $scope.stateList = [
                {id: 0, name: "未入库"},
                {id: 1, name: "部分入库"},
                {id: 2, name: "已入库"},
                {id: 3, name: "强制结单"}
            ];
            $scope.auditState = [
                {id: 0, name: "待审核"},
                {id: 1, name: "审核通过"},
                {id: 2, name: "审核驳回"}
            ];
            $scope.sapState = [
                {id: 1, name: "是"},
                {id: 0, name: "否"}
            ];
            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/saleReverse/export"),
                sectionParam: {ids: []},
                allUrl: ybHttp.RequestUrl("device", "/saleReverse/exportAll")
            };

            $scope.tableConfig = {
                fixedWidth: 240,
                scrollWidth: 1260,
                tableCol: [
                    {
                        type: "",
                        name: "冲销入库单号",
                        dataField: "reverseOrder",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        clickFn: "viewDetail",
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "发货通知单号",
                        dataField: "deliveryNoteOrder",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "客户名称",
                        dataField: "agentName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "仓库编码",
                        dataField: "warehouseCode",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "入库状态",
                        dataField: "stateShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "审核状态",
                        dataField: "auditStateShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "发货通知单是否过账",
                        dataField: "isPostingShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "SAP是否冲销成功",
                        dataField: "isSapReverseShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "创建人",
                        dataField: "createName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "制单时间",
                        dataField: "createDateShows",
                        width: 130,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "备注",
                        dataField: "note",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 200,
                        isShow: true,
                        isFixed: true,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };
            $scope.deliveryNoticeTbConf = {
                scrollWidth: 830,
                tableCol: [
                    {
                        type: "",
                        name: "发货通知单号",
                        dataField: "deliveryNo",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "客户名称",
                        dataField: "purchaseOrgName",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "结算方式",
                        dataField: "settleTypeName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "申请人",
                        dataField: "createName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "申请日期",
                        dataField: "createDateShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "出库状态",
                        dataField: "isOutDesc",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "物流状态",
                        dataField: "logState",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 80,
                        isShow: true,
                        isFixed: true,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };
            $scope.materielTbConf = {
                scrollWidth: 830,
                tableCol: [
                    {
                        name: "零件编号",
                        width: 150,
                        dataField: "materialNo"
                    },
                    {
                        name: "零件中文名",
                        width: 150,
                        dataField: "materialName"
                    },
                    {
                        name: "单位",
                        width: 100,
                        dataField: "unit"
                    },
                    {
                        name: "计划出库数量",
                        width: 150,
                        dataField: "deliveryQty"
                    },
                    {
                        name: "实际出库数量",
                        width: 150,
                        dataField: "inventoryOutAmount"
                    },
                    {
                        name: "状态",
                        width: 100,
                        dataField: "statusShow"
                    }
                ]
            };
            $scope.detailTbConf = {
                scrollWidth: 1070,
                tableCol: [
                    {
                        name: "零件编号",
                        width: 150,
                        dataField: "materialNo"
                    },
                    {
                        name: "零件中文名",
                        width: 150,
                        dataField: "materialName"
                    },
                    {
                        name: "单位",
                        width: 100,
                        dataField: "unit"
                    },
                    {
                        name: "申请发货数量",
                        width: 110,
                        dataField: "deliveryQty"
                    },
                    {
                        name: "实际发货数量",
                        width: 110,
                        dataField: "realQty"
                    },
                    {
                        name: "计划入库数量",
                        width: 110,
                        dataField: "amount"
                    },
                    {
                        name: "实际入库数量",
                        width: 110,
                        dataField: "realAmount"
                    },
                    {
                        name: "入库状态",
                        width: 100,
                        dataField: "stateShow"
                    }
                ]
            };
            $scope.auditTbConf = {
                scrollWidth: 1070,
                tableCol: [
                    {
                        name: "零件编号",
                        width: 150,
                        dataField: "materialNo"
                    },
                    {
                        name: "零件中文名",
                        width: 150,
                        dataField: "materialName"
                    },
                    {
                        name: "单位",
                        width: 100,
                        dataField: "unit"
                    },
                    {
                        name: "申请发货数量",
                        width: 110,
                        dataField: "deliveryQty"
                    },
                    {
                        name: "实际发货数量",
                        width: 110,
                        dataField: "realQty"
                    },
                    {
                        name: "计划入库数量",
                        width: 110,
                        dataField: "amount"
                    },
                    {
                        name: "实际入库数量",
                        width: 110,
                        dataField: "realAmount"
                    },
                    {
                        name: "状态",
                        width: 100,
                        dataField: "stateShow"
                    }
                ]
            };
            $scope.inTbConf = {
                scrollWidth: 1090,
                tableCol: [
                    {
                        name: "零件编号",
                        width: 150,
                        dataField: "materialNo"
                    },
                    {
                        name: "零件中文名",
                        width: 150,
                        dataField: "materialName"
                    },
                    {
                        name: "单位",
                        width: 100,
                        dataField: "unit"
                    },
                    {
                        name: "申请发货数量",
                        width: 110,
                        dataField: "deliveryQty"
                    },
                    {
                        name: "实际发货数量",
                        width: 110,
                        dataField: "realQty"
                    },
                    {
                        name: "计划入库数量",
                        width: 110,
                        dataField: "amount"
                    },
                    {
                        name: "实际入库数量",
                        width: 110,
                        dataField: "realAmount"
                    },
                    {
                        name: "储位",
                        width: 150,
                        dataField: "locationCode",
                        input: true,
                        inputType: "func"
                    },
                    {
                        name: "入库状态",
                        width: 100,
                        dataField: "stateShow",
                        input: false,
                        inputType: "",


                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    }
                ]
            };
            $scope.tabFlag = {};
            var sortParam = "";
            var sortFiled = {};

            //销售冲销类型枚举，0全部冲销，1部分冲销
            $scope.reverseType = {
                allReverse: "0",
                partReverse: "1"
            };

            $scope.currentReverseType = "";

            var btnPermissionCode = {
                salesOffsetAdd: window.ActionCode.salesOffsetAdd,
                salesOffsetEdit: window.ActionCode.salesOffsetEdit,
                salesOffsetDel: window.ActionCode.salesOffsetDel,
                salesOffsetAudit: window.ActionCode.salesOffsetAudit,
                salesOffsetExport: window.ActionCode.salesOffsetExport,
                salesOffsetIn: window.ActionCode.salesOffsetIn,
                salesOffsetPrint: window.ActionCode.salesOffsetPrint,
                salesOffsetPosting: window.ActionCode.salesOffsetPosting
            };

            $scope.btnPermission = {
                salesOffsetAdd: true,
                salesOffsetEdit: true,
                salesOffsetDel: true,
                salesOffsetAudit: true,
                salesOffsetExport: true,
                salesOffsetIn: true,
                salesOffsetPrint: true,
                salesOffsetPosting: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    billFixedWidth: {left: $scope.tableConfig.fixedWidth + "px"}
                };
            };

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false, false, false];
                $scope.activeFlag[index] = true;
                switch (index) {
                    case 0:
                        initData();
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                    case 4:
                        break;
                    default:
                        break;
                }
            };

            function initData() {
                $scope.submitted = false;
                $scope.detailInfo = {};
                $scope.toolbarConfig.showMateriel = false;
                $scope.steps.curStep = 0;
            }

            function getDataList(curPage) {
                $scope.markInfo = {
                    allChecked: false,
                    curRow: -1
                };
                $scope.exportParam.sectionParam.ids = [];
                var service = "device";
                var reqUrl = "/saleReverse/list";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                        item.stateShow = $filter("ProgressStatus")(item.state, "inDepot");
                        item.isPostingShow = $filter("TrueOrFalse")(item.isPosting);
                        item.isSapReverseShow = $filter("TrueOrFalse")(item.isSapReverse);
                        item.createDateShow = $filter("YbDate")(item.createDate);
                        item.auditStateShow = $filter("ProgressStatus")(item.auditState, "audit");
                        item.createDateShows = $filter("YbDate")(item.createDate, 'yyyy-MM-dd HH:mm:ss');
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.toSort = function (config, index) {
                // return;
                if (!config[index].toSort) {
                    return;
                }
                var sortType = ybWidget.ToSort(config, index);
                sortFiled = config[index];
                sortParam = sortFiled.sortField + " " + sortType;
                getDataList(1);
            };

            function clearSortParam() {
                sortParam = "";
                if (angular.isDefined(sortFiled.sortType)) {
                    sortFiled.sortType = ""
                }
            }

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            $scope.tdClick = function (item, fnType) {
                switch (fnType) {
                    case "viewDetail":
                        viewDetail(item);
                        break;
                    default:
                        break;
                }
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.switchDataPicker = function (index) {
                $scope.datePickerFlag = [false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {seller: true};
            };

            $scope.orderReset = function () {
                $scope.orderCondition = {seller: true};
            };


            function geBillIds() {
                $scope.exportParam.sectionParam.ids = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.exportParam.sectionParam.ids.push(item.id);
                    }
                });
            }

            $scope.selectSingle = function (item, dataList) {
                if (!item.isChecked) {
                    $scope.markInfo.allChecked = false;
                } else {
                    if (!checkAll(dataList)) {
                        $scope.markInfo.allChecked = false;
                    } else {
                        $scope.markInfo.allChecked = true;
                    }
                }
                geBillIds();
            };

            function checkAll(dataList) {
                var selectAll = false;
                for (var i = 0, len = dataList.length; i < len; i++) {
                    if (!dataList[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectAll = function (dataList) {
                for (var i = 0, len = dataList.length; i < len; i++) {
                    dataList[i].isChecked = $scope.markInfo.allChecked;
                }
                geBillIds();
            };

            $scope.printBill = function (item) {
                var id = item.id;
                var billIds = [];
                billIds.push(id);
                var param = {
                    billId: billIds,
                    printType: "SaleOffset"
                };
                ybWidget.SetIsPrintOrPreview(param);
            };

            $scope.addApply = function (reverseType) {
                $scope.markInfo.allChecked = false;
                $scope.currentReverseType = reverseType;
                getDeliveryNotice(1);
                $scope.materielTbConf.tableCol = isAddAmountFn($scope.materielTbConf.tableCol);
                $scope.changeTab(1);
            };

            //是否在表格中增加冲销数量字段方法
            function isAddAmountFn(dataList) {
                dataList = dataList.filter(function (item) {
                    return item.name !== "冲销数量"
                });
                if ($scope.currentReverseType === $scope.reverseType.partReverse) {
                    dataList.push({
                        name: "冲销数量",
                        width: 150,
                        dataField: "amount",
                        type: "input"
                    })
                }
                return dataList;
            }


            function getDeliveryNotice(curPage) {
                var service = "device";
                var reqUrl = "/deliveryNote/list";
                var param = {};
                param.pageSize = $scope.deliveryPageInfo.pageSize;
                if (!!curPage) {
                    $scope.deliveryPageInfo.curPage = curPage;
                }
                param.pageNum = $scope.deliveryPageInfo.curPage;
                param.filters = {};
                param.filters = $scope.orderCondition;
                param.filters.ticketState = 0;
                param.filters.outState = 1;
                param.filters.reverseState = 1;
                param.filters.status = '1';
                if ($scope.isNJ) {
                    param.filters.isInvoice = '0';
                }
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.createDateShow = $filter("YbDate")(item.createDate);
                    });
                    $scope.deliveryPageInfo.totalRows = resp.value.total;
                    $scope.deliveryPageInfo.totalPages = resp.value.pages;
                    $scope.deliveryNoticeList = resp.value.list;
                });
            }

            $scope.selDeliveryNotice = function (item) {
                $scope.steps.curStep = 1;
                var materialList = [];
                ybHttp.Get('device', '/deliveryNote/item', {id: item.id}).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.detailInfo.headerInfo = resp.value.headerInfo;
                    angular.forEach(resp.value.materialList, function (item) {
                        var materialItem = {};
                        materialItem.materialNo = item.materialsNo;
                        materialItem.materialName = item.materialsDes;
                        materialItem.unit = item.unit;
                        materialItem.deliveryQty = item.deliveryQty;
                        materialItem.inventoryOutAmount = item.inventoryOutAmount;
                        materialItem.amount = item.inventoryOutAmount;
                        materialList.push(materialItem);
                        materialItem.statusShow = $filter("ProgressStatus")(item.status, "outDepot");
                        materialItem.invoiceFlag = item.invoiceFlag;
                    });
                    // 农机需要过滤指定零件
                    if ($scope.isNJ) {
                        let arr = materialList.filter(item => item.invoiceFlag == 0)
                        $scope.detailInfo.detailList = arr
                    }else {
                        $scope.detailInfo.detailList = materialList;
                    }
                    
                });
            };

            $scope.orderSearch = function () {
                getDeliveryNotice();
            };

            $scope.delData = function (delType, billId) {
                var service = "device";
                var reqUrl = "/saleReverse/remove";
                var billIds = [];

                if (delType === "single") {
                    billIds.push(billId);
                }

                if (delType === "batch") {
                    for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                        if (!!$scope.dataList[i].isChecked) {
                            billIds.push($scope.dataList[i].id);
                        }
                    }
                }

                if (billIds.length === 0) {
                    ybWidget.ShowMessage('请选择需要删除的行');
                    return;
                }
                ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function () {
                    ybHttp.Post(service, reqUrl, billIds).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    });
                });
            };

            function getDetail(item) {
                var service = "device";
                var reqUrl = "/saleReverse/item";
                var param = {id: item.id};
                return ybHttp.Get(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return false;
                    }
                    return resp.value;
                    // angular.forEach(resp.value.detailList, function (item) {
                    //     item.stateShow = $filter("ProgressStatus")(item.state, "inDepot");
                    // });
                    // $scope.detailInfo = resp.value;
                    // $scope.changeTab(2);
                });
            }

            function viewDetail(item) {
                getDetail(item).then(function (data) {
                    if (!data) {
                        return;
                    }
                    angular.forEach(data.detailList, function (item) {
                        item.stateShow = $filter("ProgressStatus")(item.state, "inDepot");
                    });
                    $scope.detailInfo = data;
                    $scope.detailTbConf.tableCol = isAddAmountFn($scope.detailTbConf.tableCol);
                    $scope.changeTab(2);
                });
            }

            $scope.auditData = function (item) {
                getDetail(item).then(function (data) {
                    if (!data) {
                        return;
                    }
                    angular.forEach(data.detailList, function (item) {
                        item.stateShow = $filter("ProgressStatus")(item.state, "inDepot");
                    });
                    $scope.detailInfo = data;
                    $scope.auditTbConf.tableCol = isAddAmountFn($scope.auditTbConf.tableCol);
                    $scope.changeTab(3);
                });
            };

            $scope.saveData = function () {
                var service = "device";
                var reqUrl = "/saleReverse/add";
                var param = {
                    headerInfo: {},
                    detailList: []
                };
                param.headerInfo.agentId = $scope.detailInfo.headerInfo.purchaseOrgId;
                param.headerInfo.agentName = $scope.detailInfo.headerInfo.purchaseOrgName;
                param.headerInfo.deliveryNoteId = $scope.detailInfo.headerInfo.id;
                param.headerInfo.deliveryNoteOrder = $scope.detailInfo.headerInfo.deliveryNo;
                param.headerInfo.isPosting = $scope.detailInfo.headerInfo.isPosting;
                param.headerInfo.note = $scope.detailInfo.headerInfo.note;
                param.headerInfo.state = $scope.detailInfo.headerInfo.isOut;
                param.headerInfo.warehouseCode = $scope.detailInfo.headerInfo.outLocation;
                param.detailList = angular.copy($scope.detailInfo.detailList);

                if ($scope.currentReverseType === "1") {
                    param.headerInfo.reverseType = 1;
                    param.detailList = param.detailList.filter(function (item) {
                        return item.isChecked === true
                    });
                    var hasCheckedItemFlag = param.detailList.some(function (item) {
                        return item.isChecked === true;
                    });
                    if (!hasCheckedItemFlag) {
                        ybWidget.ShowToaster('error', '请选择零件');
                        return;
                    }
                    angular.forEach(param.detailList, function (item) {
                        if (!item.amount) {
                            item.amount = 0;
                        }
                    });
                }

                var amountLessThanOutAmountFlag = param.detailList.some(function (item) {
                    return item.qtyErr
                });
                if (amountLessThanOutAmountFlag) {
                    return;
                }

                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    getDataList(1);
                    $scope.changeTab(0);
                });
            };

            $scope.countTotal = function (item) {
                item.qtyErr = false;
                if (item.amount > item.inventoryOutAmount) {
                    item.qtyErr = true;
                }
            };

            $scope.auditPurchase = function (id, state, message) {
                var service = "device";
                var reqUrl = "/saleReverse/audit";
                if (state === 2) {
                    if (!message) {
                        ybWidget.ShowMessage('请输入审核意见');
                        $scope.submitted = true;
                        return;
                    }
                }
                var param = {
                    id: id,
                    auditState: state,
                    auditNote: message
                };
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.changeTab(0);
                    getDataList(0);
                });
            };

            $scope.prevStep = function () {
                $scope.steps.curStep = 0;
            };

            $scope.cancel = function () {
                $scope.changeTab(0);
            };

            $scope.inbound = function (item) {
                getDetail(item).then(function (data) {
                    if (!data) {
                        return;
                    }
                    $scope.detailInfo = data;
                    angular.forEach($scope.detailInfo.detailList, function (item) {
                        item.stateShow = $filter("ProgressStatus")(item.state, "inDepot");
                    });
                    $scope.changeTab(4);
                });
            };

            $scope.selDepot = function (item) {
                var param = {
                    warehouseCode: $scope.detailInfo.headerInfo.warehouseCode
                };
                ybWidget.DepotSelector(param).then(function (data) {
                    item.warehouseCode = data.warehouseCode;
                    item.areaCode = data.areaCode;
                    item.locationCode = data.locationCode;
                })
            };

            $scope.clearDepot = function (item) {
                item.warehouseCode = "";
                item.areaCode = "";
                item.locationCode = "";
            };

            $scope.inboundSubmit = function () {
                var service = "device";
                var reqUrl = "/warehouseInboundRecord/receive";
                var param = {
                    headerInfo: {},
                    detailList: []
                };
                param.headerInfo.businessOrder = $scope.detailInfo.headerInfo.reverseOrder;
                angular.forEach($scope.detailInfo.detailList, function (item) {
                    var listRow = {};
                    listRow.materialNo = item.materialNo;
                    listRow.warehouseCode = item.warehouseCode;
                    listRow.areaCode = item.areaCode;
                    listRow.locationCode = item.locationCode;
                    param.detailList.push(listRow);
                });
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.changeTab(0);
                    getDataList();
                });
            };

            $scope.posting = function (item) {
                var service = "device";
                var reqUrl = "/deliveryNote/sapReverse";
                var param = {};
                param.deliveryNo = item.deliveryNoteOrder;
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    getDataList();
                });
            };

            (function () {
                getDataList();
                $scope.setFixedWidth();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);